//
//  LBCategorySelectedView.m
//  AFNetworking
//
//  Created by luckyRoy on 2018/1/31.
//

#import "LBCategorySelectedView.h"
#import <LBFacListView.h>
#import <NSString+LBString.h>
#import "UIColor+LBColor.h"
#import "Masonry.h"
#import <NSMutableAttributedString+Common.h>
#import "UIView+LBLine.h"
#import "UIButton+LBImageTitleStyle.h"
#import "LBCategoryModel.h"

static CGFloat const LBCategorySelectedViewSingleLineViewLeftSpace = 97.0;

static CGFloat const LBCategorySelectedViewMultipleLineViewLeftSpace = 182.0;

static CGFloat const LBCategorySelectedViewLineViewHeight = 16;

static CGFloat const LBCategorySelectedViewLineViewTopSpace = 16;

@interface LBCategorySelectedView ()

@property (nonatomic, assign) BOOL isSingleSelected;

@property (nonatomic, strong) UIView *blackBgView;

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UILabel *leftLabel;

@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, copy) LBCategorySelectedViewBlock complete;

@property (nonatomic, strong) UIButton *nextButton;

@end
 
@implementation LBCategorySelectedView

- (instancetype)initWithFrame:(CGRect)frame
             isSingleSelected:(BOOL)isSingleSelected
                     complete:(LBCategorySelectedViewBlock)complete
{
    self = [super initWithFrame:frame];
    if (self) {
        self.isSingleSelected = isSingleSelected;
        self.complete = complete;
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
    [self addSubview:self.lineView];
    [self addSubview:self.leftLabel];
    [self addSubview:self.rightButton];
    [self addSubview:self.nextButton];
    
    CGFloat lineViewleftSpace = 176;
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(lineViewleftSpace);
        make.top.offset(LBCategorySelectedViewLineViewTopSpace);
        make.size.mas_equalTo(CGSizeMake(1, LBCategorySelectedViewLineViewHeight));
    }];
    
    CGFloat lineViewLeftAndRightSpace;
    
    if (self.isSingleSelected) {
        lineViewLeftAndRightSpace = 18;
    } else {
        lineViewLeftAndRightSpace = 35;
    }
    
    CGFloat defaultHeight = 46;
    
    CGFloat rightButtonWidth = 96;
    
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.lineView).offset(-lineViewLeftAndRightSpace);
        make.top.equalTo(self);
        make.height.mas_equalTo(defaultHeight);
        make.bottom.equalTo(self);
    }];
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineView.mas_right);
        make.centerY.equalTo(self.lineView);
        make.width.mas_equalTo(rightButtonWidth);
    }];
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rightButton.mas_right);
        make.top.bottom.right.equalTo(self);
    }];
    
    [self addLineIsUp:YES LeftMargin:0 RightMargin:0];
}

- (void)reloadView:(NSArray *)selectedArray isUp:(BOOL)isUp;
{
    NSArray *modelArray = [NSMutableArray arrayWithArray:selectedArray];
    
    if (self.isSingleSelected) {
        LBCategoryModel *model = [modelArray lastObject];
        [self.leftLabel setText:@"已选"];
        NSString *showStr = [NSString stringWithFormat:@"%@-%@",model.parName,model.name];
        [self.rightButton setTitle:showStr forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor blue] forState:UIControlStateNormal];
    } else {
        NSString *countNum = [@(modelArray.count)stringValue];
        NSString *leftStr = [NSString stringWithFormat:@"已选分类 %@ 个",countNum];
//        NSMutableAttributedString *showStr = [NSMutableAttributedString lb_getAttributedStringWithString:leftStr sub:countNum color:[UIColor blue]];
        [self.leftLabel setText:leftStr];
        [self.rightButton setTitle:@"查看" forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor blue] forState:UIControlStateNormal];
        if (isUp) {
            [self.rightButton setImage:[UIImage imageNamed:@"Category.bundle/lb_category_up"] forState:UIControlStateNormal];
        } else {
            [self.rightButton setImage:[UIImage imageNamed:@"Category.bundle/lb_category_down"] forState:UIControlStateNormal];
        }
        [self.rightButton lb_setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:5];
    }
}

#pragma mark - click

- (void)rightButtonClick:(UIButton *)senter
{
    if (self.complete) {
        self.complete(LBCategorySelectedViewBlockTypeLooking);
    }
}

- (void)submitButtonClick:(UIButton *)senter
{
    if (self.complete) {
        self.complete(LBCategorySelectedViewBlockTypeNext);
    }
}

#pragma mark - setter

#pragma mark - getter

- (UIView *)blackBgView
{
    if (!_blackBgView) {
        _blackBgView = [[UIView alloc]init];
        _blackBgView.backgroundColor = [UIColor blackColor];
    }
    return _blackBgView;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, LBCategorySelectedViewLineViewHeight)];
        _lineView.backgroundColor = [UIColor colorWithRed:217/255.0 green:222/255.0 blue:234/255.0 alpha:1];
    }
    return _lineView;
}

- (UILabel *)leftLabel
{
    if (!_leftLabel) {
        _leftLabel = [[UILabel alloc]init];
        _leftLabel.font = [UIFont systemFontOfSize:14];
        _leftLabel.textColor = [UIColor blue];
    }
    return _leftLabel;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setTitleColor:[UIColor black] forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (UIButton *)nextButton
{
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _nextButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _nextButton.backgroundColor = [UIColor blue];
        [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(submitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextButton;
}

@end
